<script type="text/javascript" charset="utf-8">
	$(function() {
		$('#dialog-form').load('<?php echo site_url(); ?>pinjaman/popup');
		$("#dialog-form").dialog({
			autoOpen: false,
			title: 'No Rekening',
			height: 400,
			width: 712,
			modal: true,
			buttons: {
				'OK': function() {
					var chkIdx = $('input:radio:checked').val();
					var aData = popup_table.fnGetData(chkIdx);
					$("#no_rekeningID").val($("#id"+chkIdx).val());
					$("#no_rekening").val(aData[0]);
					$("#jenis_pinj").val(aData[1]);
					$("#jenis_pinj2").val(aData[1]);
					$("#anggota_id").val(aData[2]);
					$("#nama_anggota").val(aData[3]);
					$("#pinjaman").val(aData[4]);
					$("#pinjaman2").val(aData[4]);
					//saldo_skrg
					$("#angsuran_ke").val(parseInt(aData[5])+parseInt(1));
					$("#angsuran_ke2").val(parseInt(aData[5])+parseInt(1));
					$("#total_angsuran").val(aData[6]);
					$("#total_angsuran2").val(aData[6]);
					$("#biaya").val(aData[7]);
					$("#saldo_skrg").val(formatIDR(unFormatIDR(aData[4]) - unFormatIDR($("#biaya").val())));					
					//var x = document.getElementById("tbDescription").value;
					/*
					if($("#jenis_pinj").val()=='2'){
						$("#biaya").val('10000');
					}else{
						$("#biaya").val('2000');
					}
					*/
					$(this).dialog('close');
				},
				'Batal': function() {
					$(this).dialog('close');
				}
			},
		});

		$('#no_rekening').focus(function() {
				$('#dialog-form').dialog('open');
			});

	});
	
</script>

<div class="post-title"><h2><a href="<?php echo current_url();?>"><?php echo $title; ?></a></h2></div>

<div class="post-body">

<div id="dialog-form"></div>

<?php
	echo form_open('angsuran/'.$form_act, 'id="angsuran_form"');

	echo "<div id='error' class='error-message' ";

	if($this->session->userdata('ERRMSG_ARR'))
	{
		echo ">";
		echo $this->session->userdata('ERRMSG_ARR');
		$this->session->unset_userdata('ERRMSG_ARR');
	}
	else
	{
		echo "style='display:none'>";
	}
	
	echo "</div>";

	$data['class'] = 'input';
	if ($act == 'view') $data['disabled'] = TRUE;
?>
	
	<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">		
		<tr>
			<th colspan='2'>**No Transaksi otomatis dibuat </th>			
		</tr>
		<tr>
			<th><?php echo form_label('No Transaksi**','no_transaksi'); ?></th>
			<td>
				<?php 
					$data['name'] = $data['id'] = 'no_transaksi';
					if($act=='add')
					{ 
						if($new_id == ''){						
							$data['value'] = '0000001';
						}else{						
							$data['value'] = formatIdangsuran(substr($new_id->no_transaksi, 2) + 1);
						}
					}else{
						$data['value'] = (set_value('no_transaksi')) ? set_value('no_transaksi') : $angsuran_data['no_transaksi'];
					}					
					$data['title'] = "No Transaksi tidak boleh kosong";
					
					echo form_hidden('no_transaksi',$data['value']);
					echo "<input size='50' value='$data[value]' disabled='1'/>";
					//echo form_input($data);
				?>
			</td>			
		</tr>		
		<tr>
			<th><?php echo form_label('No Rekening','no_rekening'); ?></th>
			<td>
				<?php 
					echo form_hidden('no_rekeningID',$angsuran_data['no_rekening_id']);

					//$data['name'] = $data['id'] = 'no_rekening';
					$data['value'] = (set_value('no_rekening')) ? set_value('no_rekening') : $angsuran_data['no_rekening'];
					$data['title'] = "No Rekening tidak boleh kosong";
					echo "<img src=".base_url()."/images/b_search.png><input id='no_rekening' name='no_rekening' size='30' value='$data[value]' />";
					//echo form_input($data);
				?>
			</td>					
		</tr>		
		<tr>
			<th>
				<?php echo form_label('Nama Anggota *','nama_anggota'); ?>
			</th>
			<td>
				<?php 
					$data['name'] = $data['id'] = 'anggota_id';
					//$data['name'] =  = 'anggota_id';					
					$data['value'] = (set_value('anggota_id')) ? set_value('anggota_id') : $angsuran_data['anggota_id'];
					//echo 'test : ' . $data['value'];					
					echo form_hidden('anggota_id',$data['value']);
					
					
					$data['value'] = (set_value('nama_anggota')) ? set_value('nama_anggota') : $angsuran_data['nama_anggota'];
										
					echo "<input id='nama_anggota' name='nama_anggota' size='50' class='required' value='$data[value]' disabled='1'/>";
				?>
			</td>
		</tr>
		
		<tr>
			<th><?php echo form_label('Tanggal','tgl_reg'); ?></th>
			<td>
				<?php 
					$data['name'] = 'tgl_reg';
					$data['id'] = 'datepicker2';
					if($act=='add')
					{ 
						$data['value'] = date("Y-m-d");;
					}else{					
						$data['value'] = (set_value('tgl_reg')) ? set_value('tgl_reg') : $angsuran_data['tgl_reg'];
					}
					$data['title'] = "Tanggal harus diisi dengan format dddd-mm-yy";				
					//echo form_input($data);
					echo "<input align='right' id='datepicker2' name='tgl_reg' size='10'  value='$data[value]' />";
				?>
			</td>				
		</tr>
		<tr>
			<th>
				<?php //echo form_label('Pinjaman','pinjaman'); ?>
			</th>
			<td>
				<?php 
					//$data['name'] = $data['id'] = 'pinjaman2';

					//echo $nilai;
					//data['value'] = (set_value('pinjaman')) ? set_value('pinjaman') : $angsuran_data['pinjaman'];					
					//echo form_hidden('pinjaman',$data['value']);
					
					//$data['value'] = 'Rp'.number_format(($data['value']), 0, '', '.') . ',00';
					$data['value'] = '0';
					$data['value'] = 'Rp'.number_format(($data['value']), 0, '', '.') . ',00';
					echo "<input type='hidden' name='pinjaman2' align='right' id='pinjaman2' size='50' value='$data[value]' />";
				?>
			</td>
		</tr>
		
		<tr>
			<th>
				<?php echo form_label('Jenis Pinjaman *','jenis_pinj'); ?>
			<td>
				<?php 				
					
					$data['value'] = (set_value('jenis_pinj')) ? set_value('jenis_pinj') : $angsuran_data['jenis_pinj'];
					echo "<input id='jenis_pinj' name='jenis_pinj' size='50' class='required' value='$data[value]' disabled='1'/>";
					echo "<input type='hidden' name='jenis_pinj2' align='right' id='jenis_pinj2' size='50' />";
				?>					
			</td>
		</tr>	
		
		<tr>
			<th>
				<?php echo form_label('Angsuran Ke *','angsuran_ke'); ?>
			</th>
			<td>
				<?php 
					$data['value'] = (set_value('angsuran_ke')) ? set_value('angsuran_ke') : $angsuran_data['angsuran_ke'];
					echo form_hidden('angsuran_ke',$data['value']);
					echo "<input id='angsuran_ke2' name='angsuran_ke2' size='5' class='required' value='$data[value]' disabled='1'/>";
					echo " dari ";
					$data['value'] = (set_value('total_angsuran')) ? set_value('total_angsuran') : $angsuran_data['total_angsuran'];
					echo form_hidden('total_angsuran',$data['value']);
					echo "<input id='total_angsuran2' name='total_angsuran2' size='5' class='required' value='$data[value]' disabled='1'/>";
				?>
			</td>
		</tr>		
		<tr>
			<th><?php echo form_label('Jumlah Angsuran *','biaya'); ?></th>
			<td>
				<?php 
					$data['name'] = $data['id'] = 'biaya';
					$data['value'] = (set_value('biaya')) ? set_value('biaya') : $angsuran_data['biaya'];
					$data['title'] = "Perkiraan biaya harus diisi angka";								
										
					if($act=='add')
					{ 
						$data['value'] = 0;
						?>
						<input type=text id='biaya' name='biaya' size='50' value='Rp0,00' align=right onkeypress='return numbersonly(this, event)' onBlur='this.value=formatIDR(this.value);' class='required' onClick='this.value=unFormatIDR(this.value);' maxlength='20'>
						
						<?php
					}else{					
						$data['value'] = (set_value('biaya')) ? set_value('biaya') : $angsuran_data['biaya'];
						?>
						<input type=text id='biaya' name='biaya' size='50' value='<?php echo 'Rp'.number_format(($data['value']), 0, '', '.') . ',00'; ?>' align=right onkeypress='return numbersonly(this, event)' onBlur='this.value=formatIDR(this.value);' class='required' onClick='this.value=unFormatIDR(this.value);' maxlength='20'>
						<?php
						
					}					
					
				?>
			</td>	
		</tr>
		<tr>
			<th><?php echo form_label('Sisa Angsuran *','saldo_skrg'); ?></th>
			<td>
				<?php 
					
					
					echo "<input align='right' id='saldo_skrg' name='saldo_skrg' size='50' />";
				?>
			</td>	
		</tr>		
		<tr>
			<th>
				<?php echo form_label('Keterangan','keterangan'); ?>
			</th>
			<td>
				<?php 
					$data['name'] = $data['id'] = 'keterangan';
					$data['value'] = (set_value('keterangan')) ? set_value('keterangan') : $angsuran_data['keterangan'];
					unset($data['title']);
					//echo form_textarea($data);
					echo "<textarea id='keterangan' name='keterangan' cols='50' value='$data[value]' /></textarea>";
				?>
			</td>
		</tr>	
	</table>
	
	<div class="buttons">
		<?php 
			if($act!='view')
			{ 
				echo form_submit('simpan','Simpan', "id = 'button-save'" );
				echo form_reset('reset','Reset', "id = 'button-reset'" );
				echo form_button(array('id' => 'button-cancel', 'content' => 'Batal', 'onClick' => "location.href='".site_url()."angsuran'" ));
			}
			else
			{
				echo form_button(array('id' => 'button-cancel', 'content' => 'Kembali', 'onClick' => "location.href='".site_url()."angsuran'" ));
			}
		?>				
	</div>
	
<?php echo form_close(); ?>

</div>

<script type='text/javascript'>
	//Validasi di client
	$(document).ready(function()
	{
		$('#angsuran_form').validate({
			errorLabelContainer: "#error",
			wrapper: "li",
			rules: 
			{
				no_rekening: "required",
				tgl_reg: "dateISO",				
			}
		});
	});
</script>
<?php
	function formatIdangsuran($id){		
		if (strlen($id) == 1)
		{
			return '000000' . $id;
		}
		else if (strlen($id) == 2)
		{
			return '00000' . $id;
		}
		else if (strlen($id) == 3)
		{
			return '0000' . $id;
		}
		else if (strlen($id) == 4)
		{
			return '000' . $id;
		}
		else if (strlen($id) == 5)
		{
			return '00' . $id;
		}
		else if (strlen($id) == 6)
		{
			return '0' . $id;
		}
		else {
			return $id;
		}
	}
?>